package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocWebsiteAddDTO;
import org.jsola.hr.dto.SocWebsiteUpdateDTO;
import org.jsola.hr.entity.SocWebsiteDO;
import org.jsola.hr.query.SocWebsiteQuery;
import org.jsola.hr.service.ISocWebsiteService;
import org.jsola.hr.vo.SocWebsiteListVO;
import org.jsola.hr.vo.SocWebsiteVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 社保网站与类型
 *
 * @author zhr
 */
@RestController("hrSocWebsiteController")
@RequestMapping(HrConstants.API_PATH + "/v1/socWebsites")
public class SocWebsiteController implements ISocWebsiteControllerAPI {

    @Autowired
    private ISocWebsiteService socWebsiteService;

    @Override
    @PostMapping
    public Result<SocWebsiteVO> save(@Valid @RequestBody SocWebsiteAddDTO socWebsiteAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保网站与类型权限校验

        return Result.success(
                socWebsiteService.save(socWebsiteAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocWebsiteUpdateDTO socWebsiteUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保网站与类型权限校验

        int count = socWebsiteService.update(socWebsiteUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{socWebsiteId}")
    public Result<Integer> delete(@PathVariable Long socWebsiteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保网站与类型权限校验

        int count = socWebsiteService.deleteByIds(tokenUser, socWebsiteId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] socWebsiteIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保网站与类型权限校验

        int count = socWebsiteService.deleteByIds(tokenUser, socWebsiteIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socWebsiteId}")
    public Result<SocWebsiteVO> findById(@PathVariable Long socWebsiteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保网站与类型权限校验

        return Result.success(
            socWebsiteService.selectById(socWebsiteId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocWebsiteListVO>> listAll(@Valid SocWebsiteQuery socWebsiteQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保网站与类型权限校验

        return Result.success(
            socWebsiteService.select(socWebsiteQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocWebsiteListVO>> page(@Valid SocWebsiteQuery socWebsiteQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socWebsiteService.selectPage(socWebsiteQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAllNotSitId")
    public Result<List<SocWebsiteDO>> listAllNotSitId(@RequestParam(value = "isDistinct", required = false) Boolean isDistinct) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                socWebsiteService.listAllNotSitId(isDistinct)
        );
    }

}

